function Export2VTK(volume, filename)
%%  Export 3D Volumes to VTK
% Export volume to VTK for using the data in another
% application. Export2VTK is a wrapper for WriteToVTK.  Export2VTK
% accepts two arguments 
%   1. a 3D volume (either int or float types) 
%   2. (optional) output filename.  If this is not used the user is asked
%   to create a file using MATLAB's uiputfile dialog.
%
% The export tool requires the WriteToVTK file. The file can be downloaded 
% from http://www.mathworks.com/matlabcentral/fileexchange/23416-writetovtk.
% The file is unlicenced. Author Tim Hutt, Imperial College.
%
% The Export2VTK tool can then be used to write the volume to a *.vtk
% file.
%
% >> Export2VTK(volume)
%
%   - Michael Eager <michael.eager@monash.edu>


%     Copyright © 2012-2013 Michael Eager <michael.eager@monash.edu> 
%
%     This file is part of Xglom.
% 
%     This is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <http://www.gnu.org/licenses/>.


if ndims(volume) ~= 3
    error('Export2VTK: Error, volume not 3D .')
end

    
if nargin < 3
   [fname, pathname] = uiputfile( {'*.vtk', 'VTK File (*.vtk)'}, 'Save as');
   if isequal(fname,0) || isequal(pathname,0)
       error('Export2VTK: Error, User pressed cancel for filename.')
   end
   filename = fullfile(pathname,fname);
end


% save volume to VTK
WriteToVTK(volume,filename);
